<template>
  <div>
    <div>
      <el-button type="primary" @click="openAddForm">添加网址</el-button>
    </div>
    <div style="margin-top:20px">
      <div style="padding:20px;height: 700px;" v-for="(item, i) in data" :key="i">
        <div style="border-bottom: 1px solid #333;">
          <a :href="item.url">{{ item.title }}</a>
        </div>
      </div>
    </div>
    <el-pagination :current-page.sync="queryInfo.current" :page-size="queryInfo.size" :page-sizes="[10, 20, 30, 40]"
      layout="total, prev, pager, next, jumper,sizes" :total="total" @current-change="handleCurrentChange"
      @size-change="handleSizeChange" />
    <el-dialog title="添加网址" :visible.sync="dialogVisible" width="500px" :before-close="handleClose">
      <el-form ref="addFormRef" :model="addForm" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="addForm.title" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="addForm.url" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addWebsite, getWebsite } from '@/api/website'
export default {
  name: 'Website',
  data() {
    return {
      queryInfo: {
        current: 1,
        size: 10
      },
      total: 0,
      addForm: {
        title: '',
        url: ''
      },
      data: [],
      dialogVisible: false,
    }
  },
  mounted() { },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getWebsite(this.queryInfo)
      if (res.status === 0) {
        this.data = res.data.websiteData
        this.total = res.data.total
      }
    },
    openAddForm() {
      this.dialogVisible = true
    },
    handleClose() {
      this.dialogVisible = false
      this.addform = {
        title: '',
        url: ''
      }
    },
    submit() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        const res = await addWebsite(this.addForm)
        if (res.status === 0) {
          this.$message.success(res.message);
          this.dialogVisible = false
        }
      })
    },
    handleCurrentChange() {
      this.getList()
    },
    handleSizeChange(val) {
      this.queryInfo.size = val
      this.getList()
    },
  },
}
</script>

<style lang='scss' scoped></style>